package com.itheima.cookie.cookietest4;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/servlet/aaa/cookieDemo07")
public class CookieDemo07 extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        //获取cookie对象数组
        Cookie[] cookies = req.getCookies();
        //遍历数组
        if(cookies!=null){
            for (Cookie cookie : cookies) {
                if("hobby".equals(cookie.getName())){
                    //如果一样则打印值在浏览器
                    resp.getWriter().write(cookie.getValue());
                    break;
                }
            }
        }
}

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
